package point24.util;

import java.util.*;

public class BruteSearcherCantoImp implements Searcher {


    public static void main(String[] args) {
        System.out.println(new BruteSearcherCantoImp().search(new Integer[]{1, 2, 3, 4}));
        // System.out.println(search(new int[]{1, 4, 2, 3}));
        //  System.out.println(search(new int[]{1, 1, 1, 1}));
    }


    /**
     * 确定搜索器的实现
     * 枚举运算符的种类,构造出后缀表达式
     *
     * @param arr
     * @return
     */
    public List<String> search(Integer[] arr) {

        List<String> ans = new Vector<>();
        Set<String> resultList = new TreeSet<>();
        for (int i = 0; i < 7; i++) ans.add("");

        for (int x = 0; x < 4; x++)
            for (int y = 0; y < 4; y++)
                for (int z = 0; z < 4; z++) {
                    List<String> exp = new Vector<>();
                    exp.add(OPERATORS[x]);
                    exp.add(OPERATORS[y]);
                    exp.add(OPERATORS[z]);
                    for (int e : arr) exp.add(Integer.toString(e));

                    for (int i = 0; i < MathTools.FAC[7]; i++) {
                        int[] idx = MathTools.codel(i, 7);
                        for (int j = 0; j < 7; j++) {
                            ans.set(j, exp.get(idx[j]));
                        }
                        if (Math.abs(ReversePolishNotation.eval(ans) - TARGET) < EPS) {
                            resultList.add(BinaryTree.convertRpnToNormalExpression(ans));
                        }
                    }
                }
        if (resultList.isEmpty()) return Collections.singletonList("No solution");
        return new Vector<>(resultList);
    }


}